package cn.edu.zafu.bigdata.llc;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Scanner;

public class FileOperations {

    // 将用户输入的文本写入到指定的文件中
    public static void writeToFile(String fileName, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 读取文件内容并将其打印到控制台
    public static void readFromFile(String fileName) {
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 主方法，用于测试
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文本内容：");
        String inputText = scanner.nextLine();
        String fileName = "files/202211080101.txt";

        // 将输入的文本写入文件
        writeToFile(fileName, inputText);

        // 读取文件内容并显示
        System.out.println("文件内容如下：");
        readFromFile(fileName);

        scanner.close();
    }
}
